<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary" @click="showAddCateDialogVisible">添加分类</el-button>
                </el-col>
            </el-row>
<!--表格区域-->
            <tree-table class="treetable" :data="cateList" :columns="columns"
                        :selection-type="false" :expand-type="false"
                        :show-index="true" index-text="#" border :show-row-hover="false">
                <template  v-slot:isok="slot">
                    <ib class="el-icon-success" v-if="!slot.row.cat_deleted" style="color:lightgreen"></ib>
                    <ib class="el-icon-error" v-else></ib>
                </template>

                <template v-slot:order="slot">
                    <el-tag size="mini" v-if="slot.row.cat_level===0">一级</el-tag>
                    <el-tag type="success" size="mini" v-else-if="slot.row.cat_level===1">二级</el-tag>
                    <el-tag type="warning" size="mini" v-else>三级</el-tag>
                </template>

                <template v-slot:opt="slot">
                    <el-button type="primary" icon="el-icon-edit" size="mini" @click="editCate(slot.row.cat_pid)"></el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteCate(slot.row.cat_pid)"></el-button>
                </template>
            </tree-table>
<!--            分页区域-->
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="querInfo.pagenum"
                    :page-sizes="[2, 5, 10,]"
                    :page-size="querInfo.pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </el-card>
<!--    添加分类-->
        <el-dialog
                title="添加分类"
                :visible.sync="addCateDialogVisible"
                width="40%"
                @close="closeCateDialog"
                >
<!--        提交表单-->
            <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px" class="demo-ruleForm">
                <el-form-item label="分类名称" prop="cat_name">
                    <el-input v-model="addCateForm.cat_name"></el-input>
                </el-form-item>
                <el-form-item label="父级选择" prop="cat_name">
<!--                    options用来指定数据源-->
                    <el-cascader
                            v-model="selectedKeys"
                            :options="parentCateList"
                            @change="parentCateChange"
                            :props="cascaderProps"
                            expandTrigger="hover"
                            clearable
                            change-on-select="true"
                    >
<!--                        :props="{ expandTrigger: 'hover' }"-->
                    </el-cascader>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
    <el-button @click="addCateDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addcate">确 定</el-button>
    </span>
        </el-dialog>

        <el-dialog
                title="修改"
                :visible.sync="editDialogVisible"
                width="30%"
                :before-close="handleClose">
            <span>没事莫乱点</span>
            <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
  </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "cate",
        data(){
            return{
                querInfo:{
                    type: 3,
                    pagenum:1,
                    pagesize:5,
                },
                //商品分类数据
                cateList:[],
                //s商品总数
                total:0,
                columns:[
                    {
                        label:'分类名称',
                        prop:'cat_name'

                    },
                    {
                        label: '是否有效',
                        type:"template",
                        template:"isok"
                    },
                    {
                        label: '排序',
                        type:"template",
                        template:"order"
                    },
                    {
                        label: '操作',
                        type:"template",
                        template:"opt"
                    }
                ],
                addCateDialogVisible:false,
                addCateForm:{
                    cat_name:'',
                    cat_pid:0,
                    cat_level:0
                },
                addCateFormRules:{
                    cat_name:[
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 2, max: 12, message: '长度在 2 到 12 个字符', trigger: 'blur' }
                    ],

                },
                parentCateList:[],
                cascaderProps:{
                    value:'cat_id',
                    label:'cat_name',
                    children:'children'
                },
                selectedKeys:[],
                editDialogVisible:false

            }
        },
        created() {
            this.getCateList();
        },
        methods:{
            async getCateList(){
                const {data:res}=await this.$http.get("categories",{params:this.querInfo});
                if(res.meta.status!==200){
                    return this.$message.error("获取商品分类失败")
                }
                this.cateList=res.data.result;
                this.total=res.data.total;
            },
            handleSizeChange(newSize){
                this.querInfo.pagesize=newSize;
                this.getCateList();
            },
            handleCurrentChange(newPage){
                this.querInfo.pagenum=newPage;
                this.getCateList();
            },
            showAddCateDialogVisible(){
                this.getParentCateList();
                this.addCateDialogVisible=true;
            },
            async getParentCateList(){
                const {data:res}=await this.$http.get("categories",{params: {type:2} });
                if(res.meta.status!==200){
                    return this.$message.error("获取父级分类失败");
                }
                // console.log(res);
                this.parentCateList=res.data;
                console.log(this.parentCateList)
            },
            parentCateChange(){
                if(this.selectedKeys.length>0){
                    this.addCateForm.cat_pid=this.selectedKeys[this.selectedKeys.length-1];
                    this.addCateForm.cat_level=this.selectedKeys.length;
                    return
                }
                else{
                    this.addCateForm.cat_pid=0;
                    this.addCateForm.cat_level=0;
                }
            },
            addcate(){
                this.$refs.addCateFormRef.validate(async vali=>{
                    if(!vali) return ;
                    const{data:res}=await this.$http.post("categories",this.addCateForm);
                    if(res.meta.status!==201){
                        return this.$message.error("添加失败")
                    }
                    this.$message.success("添加成功");
                    this.getCateList();
                    this.addCateDialogVisible=false;

                });
                console.log(this.addCateForm)
            },
            closeCateDialog(){
                this.$refs.addCateFormRef.resetFields();
                this.selectedKeys=[];
                this.addCateForm.cat_level=0;
                this.addCateForm.cat_pid=0;
            },
            async deleteCate(id){
                const confirmResult=await this.$confirm("此操作将永久删除该用户，是否继续？","提示",{
                    confirmButtonText:"确定",
                    cancelButtonText:"取消",
                    type:'warning'
                }).catch(err=>err);
                if(confirmResult!=="confirm"){
                    return this.$message.info("已经取消删除");
                }
                else {
                    const {data:res}=await this.$http.delete("categories/"+id);
                    if(res.meta.status!==200){
                        this.$message.error("删除失败");
                        return
                    }
                    this.$message.success("你以为的删除成功");
                    this.getCateList();
                    console.log(res);
                }
            },
            editCate(id){
                this.editDialogVisible=true;
            }

        }

    }
</script>

<style lang="less" scoped>
    .el-breadcrumb{
        padding: 15px;
    }
    .el-card{
        margin-top: 20px;
    }
    .treetable{
        margin-top: 15px;
    }
    .el-cascader{
        width: 100%;
    }
</style>
